package cn.ffcs.uec.outStaff.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Messagebox;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.raptornuke.plugin.common.zk.util.ZkUtil;
import cn.ffcs.uec.common.util.NodeVoUtil;
import cn.ffcs.uec.common.util.UecConstants;
import cn.ffcs.uec.outStaff.action.bean.UecOutStaffTrainEditBean;
import cn.ffcs.uec.outStaff.model.UecOutStaffInfo;
import cn.ffcs.uec.staff.model.UecTrain;
import cn.ffcs.uom.common.constants.BaseUnitConstants;
import cn.ffcs.uom.common.util.DateUtil;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.PlatformUtil;
import cn.ffcs.uom.common.util.PubUtil;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.NodeVo;

@Controller
@Scope("prototype")
public class UecOutStaffTrainEditComposer extends BasePortletComposer {

	private static final long serialVersionUID = 3086340593438520480L;
	/**
	 * 页面bean
	 */
	private UecOutStaffTrainEditBean bean = new UecOutStaffTrainEditBean();
	/**
	 * 操作类型
	 */
	private String opType;
	/**
	 * 人员信息
	 */
	private UecOutStaffInfo uecStaffInfo;
	/**
	 * 添加人员教育培训
	 */
	private UecTrain uecTrain;

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		Components.wireVariables(comp, bean);
	}

	/**
	 * 初始化窗口
	 * 
	 * @throws ParseException
	 */
	public void onCreate$uecTrainEditWindow() throws ParseException {
		this.bindCombobox();
		this.bindBean();
	}

	/**
	 * 绑定combobox
	 */
	private void bindCombobox() {

		// 培训类别traType
		ListboxUtils.rendererForEdit(bean.getTraType(), NodeVoUtil.getDictionaries(UecConstants.TRAIN_TYPE));
		// 培训内容类别
		ListboxUtils.rendererForEdit(bean.getGlbdef6(), NodeVoUtil.getDictionaries(UecConstants.TRAIN_CONTENT_CLASSIFY));
		// 培训方式traMode
		ListboxUtils.rendererForEdit(bean.getTraMode(), NodeVoUtil.getDictionaries(UecConstants.TRAIN_MODE));
		// 参训来源sourceType
		List<NodeVo> nodeVoSourceTypeList = new ArrayList<NodeVo>();
		String[][] sourceType = { { "0", "指定参训" }, { "1", "报名参训" },
				{ "2", "其他" }, { "3", "手工添加" }, { "4", "网大提供" },
				{ "5", "手工导入" } };
		for (int i = 0; i < sourceType.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(sourceType[i][j]);
				nodeVo.setName(sourceType[i][j + 1]);
			}
			nodeVoSourceTypeList.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getSourceType(), nodeVoSourceTypeList);
	}

	/**
	 * bindBean
	 * 
	 * @throws ParseException
	 */
	public void bindBean() throws ParseException {
		opType = (String) arg.get("opType");
		if ("add".equals(opType)) {
			this.bean.getUecTrainEditWindow().setTitle("新增人员教育培训");
			bean.getUecTrainEditDiv().setHeight("30%");
			uecStaffInfo = (UecOutStaffInfo) arg.get("uecOutStaffInfo");
		}
		if ("view".equals(opType)) {
			this.bean.getUecTrainEditWindow().setTitle("查看人员教育培训");
			uecTrain = (UecTrain) arg.get("uecTrain");
			if (null != uecTrain) {
				PubUtil.fillBeanFromPo(uecTrain, bean);
				stringTransToDate();
				bean.getButtonDiv().setVisible(false);
				PubUtil.disableComponent(bean.getUecTrainEditWindow(), true);
			}
		}
		if ("edit".equals(opType)) {
			this.bean.getUecTrainEditWindow().setTitle("编辑人员教育培训");
			bean.getUecTrainEditDiv().setHeight("30%");
			uecTrain = (UecTrain) arg.get("uecTrain");
			if (null != uecTrain) {
				PubUtil.fillBeanFromPo(uecTrain, bean);
				stringTransToDate();
			}
		}
	}

	/**
	 * 保存
	 * 
	 * @throws Exception
	 */
	public void onOk() throws Exception {
		String msg = this.checkUacStaffData();
		if (null != msg) {
			Messagebox.show(msg);
			return;
		}
		String currentName = "";
		if (StrUtil.isNullOrEmpty(PlatformUtil.getCurrentUser())) {
			ZkUtil.showError("当前登录用户不存在!", "错误提示");
			return;
		}else {
			currentName = PlatformUtil.getCurrentUser().getScreenName();
		}
		if ("add".equals(opType)) {
			uecTrain = UecTrain.newInstance();
			PubUtil.fillPoFromBean(bean, uecTrain);
			dateTransToString();
			uecTrain.setStaffId(uecStaffInfo.getOutStaffId());
			uecTrain.setNmStaffType(BaseUnitConstants.NM_OUT_STAFF_TYPE);
			uecTrain.setPkPsndoc(uecStaffInfo.getPkPsnMsg());
			
			uecTrain.setCreator(currentName);
			uecTrain.setCreationtime(DateUtil.getCurrenDate());
			
			uecTrain.addOnly();
			Events.postEvent("onOK", this.self, uecTrain);
			this.onCancel();
			Messagebox.show("新增成功！");
		}
		if ("edit".equals(opType)) {
			PubUtil.fillPoFromBean(bean, uecTrain);
			dateTransToString();
			
			uecTrain.setModifier(currentName);
			uecTrain.setModifiedtime(DateUtil.getCurrenDate());
			
			uecTrain.updateOnly();
			Events.postEvent("onOK", this.self, uecTrain);
			this.onCancel();
			Messagebox.show("更新成功！");
		}
	}

	/**
	 * 取消
	 */
	public void onCancel() {
		// 关闭窗口
		this.bean.getUecTrainEditWindow().onClose();
	}

	/**
	 * 检测填入的参数是否为空
	 */
	private String checkUacStaffData() {
		// if (StrUtil.isNullOrEmpty(bean.getAbroadway().getValue())) {
		// return "请填写出国出境目的";
		// } else if (StrUtil.isNullOrEmpty(bean.getAbroaddate().getValue())) {
		// return "请填写出国(出境)时间";
		// }else if (StrUtil.isNullOrEmpty(bean.getAbroadarea().getValue())) {
		// return "请填写所去国家";
		// }
		return null;
	}

	/**
	 * 将Date类型时间转成String类型 Date --> String 一套模型，两套标准：日期类型需要格式化
	 */
	public void dateTransToString() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		// 报名日期
		if (!StrUtil.isNullOrEmpty(bean.getSignonDt().getValue())) {
			uecTrain.setSignonDt(formatter
					.format(bean.getSignonDt().getValue()));
		}
		// 获证日期
		if (!StrUtil.isNullOrEmpty(bean.getCertificateDate().getValue())) {
			uecTrain.setCertificateDate(formatter.format(bean
					.getCertificateDate().getValue()));
		}
		// 培训开始日期
		if (!StrUtil.isNullOrEmpty(bean.getBegindate().getValue())) {
			uecTrain.setBegindate(formatter.format(bean.getBegindate()
					.getValue()));
		}
		// 培训结束日期
		if (!StrUtil.isNullOrEmpty(bean.getEnddate().getValue())) {
			uecTrain.setEnddate(formatter.format(bean.getEnddate().getValue()));
		}

	}

	/**
	 * 将String类型的时间转换成Date类型
	 * 
	 * @throws ParseException
	 */
	public void stringTransToDate() throws ParseException {
		// 时间类型转换：String --> Date
		// 报名日期
		if (null != uecTrain.getSignonDt()) {
			bean.getSignonDt().setValue(DateUtil.patternStrToDate(uecTrain.getSignonDt()));
		}
		// 获证日期
		if (null != uecTrain.getCertificateDate()) {
			bean.getCertificateDate().setValue(
					DateUtil.patternStrToDate(uecTrain.getCertificateDate()));
		}
		// 培训开始日期
		if (null != uecTrain.getBegindate()) {
			bean.getBegindate().setValue(DateUtil.patternStrToDate(uecTrain.getBegindate()));
		}
		// 培训结束日期
		if (null != uecTrain.getEnddate()) {
			bean.getEnddate().setValue(DateUtil.patternStrToDate(uecTrain.getEnddate()));
		}
	}

}
